﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using WorseGame.Common.Configs;
using WorseGame.Common.Systems;
using WorseGame.Content.Debuffs;

namespace WorseGame.Common.Players
{
    public class FishingPlayer : ModPlayer
    {
        public FishingAttempt? currentFishingAttempt;
        public bool IsCatchingFish => _isCatchingFish;
        private bool _isWaitingForGame;
        private int _hitTimer;
        private int _fishToCatch;
        private bool _isCatchingFish;

        public override bool CanUseItem(Item item)
        {
            return !_isCatchingFish;
        }

        public override bool PreItemCheck()
        {
            if (_isCatchingFish)
            {
                Player.itemAnimation = 2;

                return false;
            }
            return true;
        }

        public override void PostItemCheck()
        {

            Item selectedItem = Player.inventory[Player.selectedItem];
            if (_isCatchingFish && selectedItem.fishingPole > 0)
            {
                Rectangle heldItemFrame = Item.GetDrawHitbox(selectedItem.type, Player);

                Player.itemLocation.X = Player.position.X + Player.width * 0.5f + heldItemFrame.Width * 0.18f * Player.direction;
                Player.itemLocation.Y = Player.position.Y + 28f + Player.HeightOffsetHitboxCenter;
            }
        }

        public override void PostUpdate()
        {
            if (SpecialConfig.Instance.FishingMinigame)
            {
                if (_isCatchingFish)
                {
                    Player.AddBuff(ModContent.BuffType<ReelingDebuff>(), 5);
                }

                if (!_isWaitingForGame)
                {
                    return;
                }

                if (_isCatchingFish && _hitTimer >= 0)
                {
                    _hitTimer--;
                }
                else if (_isCatchingFish && _hitTimer <= 0)
                {
                    _hitTimer = 0;
                    _isWaitingForGame = false;

                    FishingUISystem fishingUISystem = ModContent.GetInstance<FishingUISystem>();
                    MinigameFishDataSystem minigameFishDataSystem = ModContent.GetInstance<MinigameFishDataSystem>();

                    fishingUISystem.BeginMinigame(minigameFishDataSystem.GetDataOrDefault(_fishToCatch), Player);
                }
            }
        }

        public override void UpdateDead()
        {
            if (_isCatchingFish)
            {
                FinishCatchingFish(false, false);
            }
        }
        public void StartCatchingFish(Projectile bobber)
        {
            _isCatchingFish = true;

            _hitTimer = 60;
            _isWaitingForGame = true;
            _fishToCatch = (int)bobber.ai[1];
            SoundEngine.PlaySound(SoundID.Item129);
            CombatText.NewText(new Rectangle((int)bobber.position.X, (int)bobber.position.Y, bobber.width, bobber.height), Color.Orange, Language.GetTextValue("Mods.WorseGame.NewText.Fish"), true);
        }
        public void FinishCatchingFish(bool successful, bool caughtCrate)
        {
            FishingUISystem fishingUISystem = ModContent.GetInstance<FishingUISystem>();

            for (int i = 0; i < Main.maxProjectiles; i++)
            {
                Projectile projectile = Main.projectile[i];
                if (!projectile.active || projectile.owner != Player.whoAmI || !projectile.bobber)
                {
                    continue;
                }

                if (successful)
                {
                    projectile.ai[0] = 1f;
                    projectile.ai[1] = _fishToCatch;
                }
                else
                {
                    projectile.Kill();
                }
            }

            if (caughtCrate && currentFishingAttempt is not null)
            {
                TreasureDeterminationSystem treasureDeterminationSystem = ModContent.GetInstance<TreasureDeterminationSystem>();

                Player.QuickSpawnItem(new EntitySource_FishedOut(Player), treasureDeterminationSystem.RetrieveTreasure(Player, currentFishingAttempt.Value));
            }

            _isCatchingFish = false;
            _fishToCatch = -1;
            currentFishingAttempt = null;
            Player.itemAnimation = 0;

            fishingUISystem.EndMinigame();
        }
    }
}